<?php

if (!defined('IN_SYS'))
    exit('Access Denied');

class yuming_controller extends admincp {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        config::update_config();
        $name = gp("yuming_name");
        $where = "";
        $yuming_name = gp("yuming_name");
        if ($yuming_name) {
            $where = " where yuming.name like '%$yuming_name%' ";
        }
        $pg = new page("select * from yuming  $where order by id desc");
        $list = $pg->get_list(300);
        $page = $pg->get_page();
        require self::tpl('yuming');
    }

    public function add() {
        if (IS_POST) {
            $v = gp("name,description,sitename,keywords,template,siteTitle");
            $ymName = trim($v['name']);
            $res = db::find("select * from yuming where name='$ymName'");
            if ($res) {
                self::json(array('status' => 1, 'msg' => '此域名已经添加'));
            } else {
                db::query("insert into yuming (name,description,sitename,siteTitle,keywords,template,mobileSwitch,autoCatch) 
					values ('$ymName', '$v[description]','$v[sitename]','$v[siteTitle]', '$v[keywords]', '$v[template]',0,0) ");
                $sid = db::insert_id();

                $contentSql = "CREATE TABLE `content_{$sid}` (
                        `id` int(11) NOT NULL AUTO_INCREMENT,
                        `class` varchar(30) DEFAULT NULL,
                        `title` varchar(255) DEFAULT NULL,
                        `flag` char(1) DEFAULT NULL,
                        `source` varchar(50) DEFAULT NULL,
                        `thumb` varchar(255) DEFAULT NULL,
                        `keyword` varchar(255) NOT NULL,
                        `description` varchar(500) DEFAULT NULL,
                        `status` int(11) DEFAULT '0',
                        `click` int(11) DEFAULT '0',
                        `addtime` int(11) DEFAULT '0',
                        `ping_status` int(11) DEFAULT '0',
                        `ping_errMsg` varchar(255) DEFAULT NULL,
                        `seo_keyword` text,
                        `oldtitle` varchar(255) DEFAULT NULL,
                        PRIMARY KEY (`id`),
                        KEY `flag` (`flag`),
                        KEY `title` (`title`),
                        KEY `addtime` (`addtime`),
                        KEY `sycc` (`class`,`flag`,`status`,`addtime`) USING BTREE,
                        KEY `syct` (`class`,`status`,`addtime`) USING BTREE,
                        KEY `oldtitle` (`oldtitle`) USING BTREE
                      ) ENGINE=MyISAM AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;";
                $word_sql = "CREATE TABLE `words_{$sid}` (
                    `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
                    `title` varchar(255) NOT NULL DEFAULT '',
                        PRIMARY KEY (`id`),
                        KEY `idx_title` (`title`) USING BTREE
                      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;";
                $taglist_sql = "CREATE TABLE `tagslist_{$sid}` (
                        `id` int(11) NOT NULL AUTO_INCREMENT,
                        `tagsname` varchar(255) DEFAULT NULL,
                        `yuming_id` int(11) DEFAULT NULL,
                        `addtime` int(11) DEFAULT NULL,
                        `click` int(11) NOT NULL DEFAULT '0',
                        PRIMARY KEY (`id`),
                        KEY `tagsname` (`tagsname`),
                        KEY `yuming_id` (`yuming_id`)
                      ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
                $tagindex_sql = "CREATE TABLE `tagindex_{$sid}` (
                        `aid` int(11) NOT NULL DEFAULT '0',
                        `tagid` int(11) NOT NULL DEFAULT '0',
                        PRIMARY KEY (`aid`,`tagid`)
                      ) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
                db::query($contentSql);
                db::query($word_sql);
                db::query($taglist_sql);
                db::query($tagindex_sql);

                $classlist = db::find("select c.* from classlist as c where c.yuming_id='$sid' order by c.weight,id");
                if (empty($classlist)) {
                    $addsql = "";
                    foreach ($this->sysclass as $ename => $title) {
                        $addsql .= (empty($addsql) ? "" : ",") . "('$sid','$ename','$title','1')";
                    }
                    if ($addsql) {
                        $addsql = "insert into classlist(yuming_id,ename,title,ifsys)values" . $addsql;
                        db::query($addsql);
                    }
                }

                self::json(array('status' => 1, 'msg' => '添加成功', 'redirect' => '?m=yuming'));
            }
        } else {
            //获取文件夹名字
            $array_file = ymcms::get_themes();
            require self::tpl("yuming_add");
        }
    }

    public function edit() {
        $id = gp('id');
        if (IS_POST) {
            $v = gp("name,description,sitename,siteTitle,keywords,template");
            db::query("update yuming set name='$v[name]', description='$v[description]',sitename='$v[sitename]', siteTitle='$v[siteTitle]',keywords='$v[keywords]' , template='$v[template]' where id='$id' ");
            self::json(array('status' => 1, 'msg' => '修改成功', 'redirect' => '?m=yuming'));
        } else {
            //获取文件夹名字
            $array_file = ymcms::get_themes();
            $r = db::find("select * from yuming where id='$id' ");
            require self::tpl("yuming_edit");
        }
    }

    public function del() {
        $tm_id = gp('id');
        if(empty($tm_id)) {
            self::json(['suc' => 0]);
        }
        if(is_string($tm_id)) {
            $ids[]=$tm_id;
        }elseif(is_array($tm_id)) {
            $ids = $tm_id;
        }
        $pass = gp('pass');
        $userid = $this->user['id'];
        $rs = db::find('select * from `admin` where id=' . $userid);
        if (empty($pass) || admin::check_passwd($rs, $pass) == false) {
            self::json(['suc' => 0, 'message' => '管理员密码不正确']);
        }
        foreach ($ids as $id) {
            $ym = db::find("select * from yuming where id = '$id'");
            if ($ym) {
                db::query("delete from catch where yuming_id ='$id'");
                db::query("delete from catchlinks where yuming_id ='$id'");
                db::query("delete from flink where yuming_id ='$id'");
                db::query("delete from classlist where yuming_id ='$id'");
                db::query("delete from reword where yuming_id ='$id'");
                db::query("delete from seoconfig where  yuming_id ='$id'");
                db::query("delete from yuming where id='$id' ");
                db::query("drop table IF EXISTS `tagslist_{$id}` ");
                db::query("drop table IF EXISTS `tagindex_{$id}` ");
                db::query("drop table IF EXISTS `content_{$id}` ");
                db::query("drop table IF EXISTS `words_{$id}` ");
            }
        }
        self::json(['suc' => 1]);
    }

    public function select() {
        require self::tpl("yuming_select");
    }

    // 手机站开关
    public function switchval() {
        $yuming_id = gp("yuming_id");
        $ms = gp("data");
        $result = db::query("update yuming set mobileSwitch='$ms' where id='$yuming_id' ");
        self::json(array('status' => 1, 'msg' => '修改成功', 'redirect' => '?m=yuming'));
    }

    // 自动采集
    public function switchcaiji() {

        $data = array();
        $data['version'] = $GLOBALS['G']['softversion'];
        $data['appid'] = $GLOBALS['G']['appid'];
        $data['http_host'] = $_SERVER['HTTP_HOST'];
        $data['autoCatch'] = gp("data");
        $data['allinone'] = 1; //新版本使用一次性触发新闻采集
        $yuming_id = gp("yuming_id");
        $webhost = db::find("select * from yuming where id='$yuming_id'");
        $data['webhost'] = $webhost['name'];
        $result = db::query("update yuming set autoCatch='$data[autoCatch]' where id='$yuming_id'");
        $msg = "";
        $resArr['status'] = 1;
        $resArr['msg'] = "切换成功";
        $resArr['redirect'] = '?m=yuming';
        self::json($resArr);
    }
    
    // 自动采集
    public function switchjsc() {

        $data = array();
        $data['version'] = $GLOBALS['G']['softversion'];
        $data['appid'] = $GLOBALS['G']['appid'];
        $data['http_host'] = $_SERVER['HTTP_HOST'];
        $data['autoJsc'] = gp("data");
        $data['allinone'] = 1; //新版本使用一次性触发新闻采集
        $yuming_id = gp("yuming_id");
        $webhost = db::find("select * from yuming where id='$yuming_id'");
        $data['webhost'] = $webhost['name'];
        $result = db::query("update yuming set autojsc='$data[autoJsc]' where id='$yuming_id'");
        $msg = "";
        $resArr['status'] = 1;
        $resArr['msg'] = "切换成功";
        $resArr['redirect'] = '?m=yuming';
        self::json($resArr);
    }

    
    

    public function tocaiji() {
        $ids = gp("ids");
        $close = intval(gp('close'));
        if (!empty($ids)) {
            if ($close == 1) {
                $autoCatch = 0;
            } else {
                $autoCatch = 1;
            }
            foreach ($ids as $id) {
                $result = db::query("update yuming set autoCatch={$autoCatch} where id={$id}");
            }
            self::json(array('status' => 1, 'msg' => '修改成功'));
        }
        self::json(array('status' => 0, 'msg' => '修改失败'));
    }
    
    public function tojsc() {
        $ids = gp("ids");
        $close = intval(gp('close'));
        if (!empty($ids)) {
            if ($close == 1) {
                $autojsc = 0;
            } else {
                $autojsc = 1;
            }
            foreach ($ids as $id) {
                $result = db::query("update yuming set autojsc={$autojsc} where id={$id}");
            }
            self::json(array('status' => 1, 'msg' => '修改成功'));
        }
        self::json(array('status' => 0, 'msg' => '修改失败'));
    }

}
